<?php

namespace app\common\controller;

use app\common\keloop\Delivery;
use app\common\library\Auth;
use think\Config;
use think\Controller;
use think\Db;
use think\Hook;
use think\Lang;
use think\Loader;
use think\Log;
use think\Validate;

/**
 * Class Common
 * @package app\common\controller
 * 公共方法
 */
class Common extends Controller
{

    public static function orderRefundNumber(){
        return "tk".time().rand(100000,999999);
    }

    public static function orderNumber(){
        return "wx".time().rand(100000,999999);
    }

    /**
     * @return string
     * 生成门牌号
     */
    public static function getShopNo(){
        $config = Delivery::getDadaSet();
        return $config['source_id'].'-'.rand(1000000,9999999);
    }

    public static function orderRefund($row,$refund_number){

        $data = [
            'amount'=> $row->actual_price,//订单金额
            'refund_money'=> $row->actual_price,//退款金额
            'orderid'=>  $row->order_number,//"订单号",
            'refund_sn'=>$refund_number,
            'type' =>  "wechat",//wechat 微信  alipay支付宝
            'title'=> '取消订单',
            'notify_url'=> request()->domain() . "/api/wechat/orderCancelNotifyx",//"回调地址",
            'method'=>"miniapp", //支付环境  miniapp(微信小程序)
        ];
        $res =  \addons\epay\library\Service::submitRefund($data);
        if($res['result_code'] != 'SUCCESS'){
            Log::log([
                '取消订单失败sj',
                'msg'=>$res
            ]);
        }
        return $res;
    }

    /**
     * @param $code
     * @return string
     * 微信退款返回的常用状态，其它还有需要请自行添加
     */
    public static function wechatCode($code){
        switch ($code){
            case 'NOTENOUGH':
                $result = '商家余额不足,请联系商家';
                break;
            case 'SYSTEMERROR':
                $result = '系统超时，请稍后重试';
                break;
            case 'TRADE_OVERDUE':
                $result = '订单已经超过退款期限';
                break;
            case 'USER_ACCOUNT_ABNORMAL':
                $result = '用户帐号注销';
                break;
            case 'INVALID_REQ_TOO_MUCH':
                $result = '无效请求过多';
                break;
            default:
                $result = '操作失败请联系管理员';
                break;
        }
        return $result;
    }

}
